require(['jquery','token','common/data','sweetalert'], function($,token,syncData,swal) {
    var pageData=syncData.fetch();
    $.each(pageData.shop.dishes,function(i,obj){
        obj.num=0;
    })
    pageData.selectItem=[];
    pageData.comment='';
    var content=new Vue({
        el: '.content',
        data:pageData,
        filters:{
            currency: {
                read: function (val) {
                    val=+val;
                    return val.toFixed(2)
                },
                // write 函数会在数据写入到模型之前被调用
                write: function (val, oldVal) {
                    var number = +val.replace(/[^\d.]/g, '')
                    return isNaN(number) ? 0 : parseFloat(number.toFixed(2))
                }
            },
            shopLink: function(val){
                console.log(val);
                var shopId=val;
                return '/shop/'+val;
            }

        },
        computed:{
            totalPrice:function(){
                var selected=this.$data.selectItem;
                var dishArr=this.$data.shop.dishes;
                var result=0;
                $.each(selected,function(i,value){
                    result+=dishArr[value].price*dishArr[value].num;
                })
                return result;
            },
            totalNum:function(){
                var selected=this.$data.selectItem;
                var dishArr=this.$data.shop.dishes;
                var result=0;
                $.each(selected,function(i,value){
                    result+=dishArr[value].num;
                })
                return result;
            }
        },
        methods:{
            reduce:function(index){
                if(this.$data.shop.dishes[index].num>0) {
                    this.$data.shop.dishes[index].num--;
                }
            },
            add:function(index){
                this.$data.shop.dishes[index].num++;
            },
            submit:function(event){
                var totalPrice=+this.totalPrice;
                var totalNum=+this.totalNum;
                if(totalPrice==0||totalNum==0) {
                    swal("Oops...", "数量或者价格为0不能生成订单", "error");
                    event.preventDefault();
                }else if(!this.$data.userBaseInfo){
                    swal("Oops...", "请登录", "error");
                    event.preventDefault();
                }
            },
            commentSubmit:function(){
                if(!this.$data.comment){
                    swal("Oops...", "评论不能为空", "error");
                }else if(!this.$data.userBaseInfo){
                    swal("Oops...", "请登录", "error");
                }else{
                    $.ajax({
                        url:'/api/comment',
                        method:'post',
                        dataType:'json',
                        data:{
                            from_user:this.$data.userBaseInfo._id,
                            to_shop:this.$data.shop._id,
                            content:this.$data.comment,
                        },
                        success:function(data){
                            if(data.status=='success'){
                                window.location.reload();
                            }
                        }
                    })
                }
            }
        }
    })
});